All files / core/util VisibilityMonitor.ts

59.38% Statements 19/32
38.89% Branches 7/18
80% Functions 4/5
56.67% Lines 17/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                                12x 12x             12x     12x 31x       31x     31x       31x   31x 31x                                 31x   31x 31x                                   12x 31x 31x   12x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { EventEmitter } from './EventEmitter';
import { assert } from '@firebase/util';
 
declare const document: any;
 
/**
 * @extends {EventEmitter}
 */
export class VisibilityMonitor extends EventEmitter {
  private visible_: boolean;
 
  static getInstance() {
    return new VisibilityMonitor();
  }
 
  constructor() {
    super(['visible']);
    let hidden: string;
    let visibilityChange: string;
    Eif (
      typeof document !== 'undefined' &&
      typeof document.addEventListener !== 'undefined'
    ) {
      Eif (typeof document['hidden'] !== 'undefined') {
        // Opera 12.10 and Firefox 18 and later support
        visibilityChange = 'visibilitychange';
        hidden = 'hidden';
      } else if (typeof document['mozHidden'] !== 'undefined') {
        visibilityChange = 'mozvisibilitychange';
        hidden = 'mozHidden';
      } else if (typeof document['msHidden'] !== 'undefined') {
        visibilityChange = 'msvisibilitychange';
        hidden = 'msHidden';
      } else if (typeof document['webkitHidden'] !== 'undefined') {
        visibilityChange = 'webkitvisibilitychange';
        hidden = 'webkitHidden';
      }
    }
 
    // Initially, we always assume we are visible. This ensures that in browsers
    // without page visibility support or in cases where we are never visible
    // (e.g. chrome extension), we act as if we are visible, i.e. don't delay
    // reconnects
    this.visible_ = true;
 
    Eif (visibilityChange) {
      document.addEventListener(
        visibilityChange,
        () => {
          const visible = !document[hidden];
          if (visible !== this.visible_) {
            this.visible_ = visible;
            this.trigger('visible', visible);
          }
        },
        false
      );
    }
  }
 
  /**
   * @param {!string} eventType
   * @return {Array.<boolean>}
   */
  getInitialEvent(eventType: string): boolean[] {
    assert(eventType === 'visible', 'Unknown event type: ' + eventType);
    return [this.visible_];
  }
}